package com.tdk.bts.dao;

import com.tdk.bts.entity.Theater;
import com.tdk.bts.util.DruidConnectionUtil;

import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TheaterServiceDao {
    private static Connection conn = null;

    static {
        if (conn == null){
            try{
                conn = DruidConnectionUtil.getConnection();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    //插入新放映厅
    public void insertTheater(String name, int rows, int cols) throws SQLException {
        TheaterServiceDao theaterServiceDao = new TheaterServiceDao();
        String sql1 = "insert into t_theater(id,name,rowx,col) values(NULL,?,?,?)";
        PreparedStatement preSt = conn.prepareStatement(sql1);
        int num = theaterServiceDao.findCountByTheaterName(name);
            if (num > 0){
                JOptionPane.showMessageDialog(null, "放映厅已存在", "提示信息", JOptionPane.INFORMATION_MESSAGE);

            } else {

                try {

                    preSt.setString(1, name);
                    preSt.setInt(2, rows);
                    preSt.setInt(3, cols);
                    preSt.addBatch();
                    JOptionPane.showMessageDialog(null,"保存信息成功","提示信息",JOptionPane.INFORMATION_MESSAGE);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            preSt.executeBatch();
            DruidConnectionUtil.closeAll(preSt, null);
        }

    //查找所有放映厅
    public List<Theater> findAllTheater() throws SQLException {
        String sql = "select * from t_theater";
        PreparedStatement preSt = conn.prepareStatement(sql);
        ResultSet rs = preSt.executeQuery();
        List<Theater> list = new ArrayList<>();
        while (rs.next()){
            Theater theater = new Theater();
            theater.setId(rs.getInt("id"));
            theater.setName(rs.getString("name"));
            theater.setColumns(rs.getInt("col"));
            theater.setRows(rs.getInt("rowx"));
            list.add(theater);
        }
        DruidConnectionUtil.closeAll(preSt,rs);
        return list;
    }

    //放映厅分页查询
    public List<Theater> findTheaters(int pageNo) throws SQLException{
        List<Theater> list = new ArrayList<>();
        Theater theater = null;
        int m = (pageNo-1)*5;
        String sql ="select * from t_theater limit ?,?";
        PreparedStatement preSt = conn.prepareStatement(sql);
        preSt.setInt(1,m);
        preSt.setInt(2,5);
        ResultSet rs = preSt.executeQuery();
        while (rs.next()){
            theater = new Theater();
            int id = rs.getInt("id");
            theater.setId(id);
            String name = rs.getString("name");
            theater.setName(name);
            int rowx = rs.getInt("rowx");
            theater.setRows(rowx);
            int col = rs.getInt("col");
            theater.setColumns(col);
            list.add(theater);
        }
        DruidConnectionUtil.closeAll(preSt,rs);
        return list;
    }

    //
    public int ThreatTotalCount() throws SQLException {
        String sql = "select COUNT(id) from t_theater";
        PreparedStatement preSt = conn.prepareStatement(sql);
        ResultSet rs = preSt.executeQuery();
        int num = 0;
        if (rs.next()) {
            num = rs.getInt(1);
        }
        DruidConnectionUtil.closeAll( preSt, rs);
        return num;
    }

    //删除放映厅
    public void deleteChosenTheater(int id) throws SQLException {
        TheaterServiceDao theaterServiceDao = new TheaterServiceDao();
        if(theaterServiceDao.compareId(id) >0){
           JOptionPane.showMessageDialog(null,"放映厅已排片，无法删除","提示信息",JOptionPane.INFORMATION_MESSAGE);
        }else{
            String sql ="delete from t_theater where id=?";
            PreparedStatement preSt = conn.prepareStatement(sql);
            preSt.setInt(1, id);
            preSt.execute();
        }
    }

    //判断id与theater_id是否一致
    public int compareId(int id) throws SQLException {
        //boolean m;
        int count =0;
        String sql = "select COUNT(s.theater_id) from t_theater  t INNER JOIN t_film_screen s on t.id = s.theater_id " +
                "AND t.id =" + id;
        PreparedStatement preSt = conn.prepareStatement(sql);
        ResultSet rs = preSt.executeQuery();
        if(rs.next()){
           count=rs.getInt(1);
        }
       return count;
    }

    //通过theater的name找出数量
    public static int findCountByTheaterName(String name) throws SQLException {
        String sql = "select COUNT(name) from t_theater where name =?";
        PreparedStatement preSt = conn.prepareStatement(sql);
        preSt.setString(1,name);
        ResultSet rs = preSt.executeQuery();
        int m = 0;
        if (rs.next()){
            m = rs.getInt(1);
            preSt.addBatch();
        }
        DruidConnectionUtil.closeAll(preSt,rs);
        return m;
    }

    public Theater findTheaterByScreenIds(int id) throws SQLException {
        Theater theater = null;
        String sql = "select theater.name ,theater.id,theater.rowx,theater.col " +
                "from t_theater theater,t_film_screen screen " +
                "where screen.theater_id = theater.id AND screen.id =?";
        PreparedStatement preSt = conn.prepareStatement(sql);
        preSt.setInt(1,id);
        ResultSet rs = preSt.executeQuery();
        while (rs.next()){
            theater = new Theater();
            Integer theaterId = rs.getInt("theater.id");
            theater.setId(theaterId);
            String name = rs.getString("theater.name");
            theater.setName(name);
            Integer rowx = rs.getInt("theater.rowx");
            theater.setRows(rowx);
            Integer col = rs.getInt("theater.col");
            theater.setColumns(col);
        }
        return theater;
    }
}

